
--开发过程中 测试模块，各种快捷键--- 服务器


local ServerOHOH = _G.ServerOHOH;

local EventList = ServerOHOH.load.EventList();
EventList.DataDebug=true

local DataBaseManager = ServerOHOH.load.DataBaseManager();
local OHOHEnum = ServerOHOH.load.OHOHEnum();


local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent());

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, ...)
	M.super.ctor(self, ...);
end;


function M.Debug(self,actionId,textArg,otherArg)
	local fuc=self[actionId]
	if fuc then
		fuc(self,textArg,otherArg)
	end
end
function M.Skibi_killed(self)


end

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("player_data")
local key = "1240761578"

function setData(userId,data)
	local success, result = pcall(function()
	  dataStore:SetAsync(userId, data)
	end)
	if not success then
	  warn(result)
	else
		print("重置游戏数据成功！")
	end
  end


function getVersionAtTime(userId,timestamp)
	local success, result, keyInfo = pcall(function()
	  return dataStore:GetVersionAtTimeAsync(userId, timestamp.UnixTimestampMillis)
	end)
	if success then
	  if result == nil then
		print("No version found at time")
	  else

		print("-------------旧数据------------------>")
		return result
	  end
	else
	  warn(result)
	end
  end

function M.FindData(self)


	



	
	-- Prints "No version found at time"
	local time1 = DateTime.fromUniversalTime(2024, 12, 02, 05, 00)
	local data=getVersionAtTime("1240761578",time1)
	if data then
		setData("1240761578",data)
	end

end


function M.FindData_old(self)
	local DataStoreService = game:GetService("DataStoreService")
	local myDataStore = DataStoreService:GetDataStore("player_data")
	
	local playerKey = 1240761578 -- 用户的唯一键
	local timestamp = os.time() - (12 * 60 * 60) -- 一天前的时间戳，调整为你需要的时间点
	local time1 = DateTime.fromUniversalTime(2024, 12, 02, 05, 00)
	local success, oldData = pcall(function()
		return myDataStore:GetVersionAtTimeAsync(playerKey, timestamp)
	end)
	
	if success then
		if oldData then
			print("成功找回数据: ", oldData)

			ServerOHOH:PrintTable(oldData)
			-- 你可以在这里将找回的数据重新存储到 DataStore 中
			-- local saveSuccess, saveError = pcall(function()
			-- 	myDataStore:SetAsync(playerKey, oldData)
			-- end)
			-- if saveSuccess then
			-- 	print("数据已恢复并保存！")
			-- else
			-- 	warn("保存恢复数据时出错: ", saveError)
			-- end
		else
			print("找不到指定时间点的数据。可能该时间点没有存储数据。")
		end
	else
		warn("获取数据时出错: ", oldData)
	end
	
end


function M.ChangeDataByUserId(self,textArg,otherArg)
	local userId=tonumber(textArg)
	local success, userData = DataBaseManager:Load(OHOHEnum.data_base.player_data, userId);
	if success then
		print("获取到玩家数据-->")
		ServerOHOH:PrintTable(userData)
		print("设置新玩家数据--->")
		local DataBase=self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())
		DataBase:SetReal(userData)
		DataBase:Print()
	end

end


function M.KillBoss(self,textArg,otherArg)

	local num=tonumber(textArg)
	if num>0 then

		local DataBase=self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())
		for levelId=1,num do
			local levels= DataBase:GetData("levels")
			if not levels[levelId] then
				levels[levelId]=1
			else
				levels[levelId]=levels[levelId]+1
			end
		end
		self.parent:SynchDataToClient("levels")
	end

end
function M.PrintTableBro(self,textArg,otherArg)

	local DataBase=self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())

	ServerOHOH:PrintTable(DataBase:GetData("broEquip").__real)
end




function M.AddLucky(self,textArg,otherArg)
	local num=tonumber(textArg)
	self.parent:GetComponent(ServerOHOH.load.Server_ItemCtrl()):AddItem({Points_lucky=num},true,true)
end

function M.ClearDataByUserID(self,textArg,otherArg)
	DataBaseManager:Clear(OHOHEnum.data_base.player_data, tonumber(textArg), function()
	end);
end

function M.ClearData(self,textArg,otherArg)
	self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent()):Clear()
end
	
function M.Dead(self,textArg,otherArg)
	self.parent.character:GetHumanoid().Health=0

end

function M.AddMeat(self,textArg,otherArg)
	local num=tonumber(textArg)
	self.parent:GetComponent(ServerOHOH.load.Server_ItemCtrl()):AddItem({base_meat=num},true,true)

end
function M.AddPower(self,textArg,otherArg)
	local num=tonumber(textArg)
	self.parent:GetComponent(ServerOHOH.load.Server_ItemCtrl()):AddItem({base_power=num},true,true)
end

function M.AddWins(self,textArg,otherArg)
	local num=tonumber(textArg)
	self.parent:GetComponent(ServerOHOH.load.Server_ItemCtrl()):AddItem({base_wins=num},true,true)
end

function M.AddDiamond(self,textArg,otherArg)
	local num=tonumber(textArg)
	self.parent:GetComponent(ServerOHOH.load.Server_ItemCtrl()):AddItem({base_diamond=num},true,true)
end
--加经验
function M.AddExp(self,textArg,otherArg)
	local num=tonumber(textArg)
	self.parent:GetComponent(ServerOHOH.load.Server_ItemCtrl()):AddItem({base_exp=num},true,true)

end

function M.AddPoint(self,textArg,otherArg)
	local num=tonumber(textArg)

	local DataBase=self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())

	DataBase:GetData("Points").had=DataBase:GetData("Points").had+num

	self.parent:SynchDataToClient("Points")
end

function M.PrintTestALLDATA(self,textArg,otherArg)
	self.parent:GetComponent(_G.CommonOHOH.load.Common_ActionSynchronize()):PrintTestALLDATA()
end

function M.PrintUserData(self,textArg,otherArg)
	local data=self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())

	-- ServerOHOH:PrintTable(data:GetData("levels"))

	data:Print()

    -- print("=====================A============================>>>")
	-- print(data:GetData("levels")[1].count)
	-- print(data:GetData("levels")[1].userId)

	-- ServerOHOH:PrintTable(data:GetData("levels")[1].statistics)

	-- print("=====================B============================>>>")

    -- ServerOHOH:PrintTable(data:GetData("levels")[11])

end

function M.DebugCanUse(self,textArg,otherArg)
	print("---------DebugCanUse------------------",textArg,otherArg)
end

function M.AddBadge(self,textArg,otherArg)
	self.parent:GetComponent(ServerOHOH.load.Server_BadgeCtrl()):AddBadge(tonumber(textArg))
end

function M.AddLevel(self,textArg,otherArg)
	self.parent:GetComponent(ServerOHOH.load.Server_ItemCtrl()):AddItem({base_level=textArg})
end

function M.dtor(self)
	M.super.dtor(self);
end;


local TEST_ADTA=require(script.Parent.data)
if TEST_ADTA.isTest then
	M:RegistEvent(EventList.DataDebug,M.Debug)
end

return M;

